//处理tree数据结构
export function formatOrganizationTreeData(data, addClassIds, historyClassId) {
  function formatNode(node, level) {
    const idMap = {
      1: node.branchId,
      2: node.majorId,
      3: node.classId
    }
    const nameMap = {
      1: node.branchName,
      2: node.majorName,
      3: node.className
    }
    const nodeId = idMap[level]
    const nodeName = nameMap[level]

    const formattedNode = {
      ...node,
      name: nodeName,
      id: nodeId,
      level,
      disabled: (addClassIds.includes(nodeId) || historyClassId.includes(nodeId)) && level === 3
    }

    if (level < 3) {
      formattedNode.children = (node.branchList || node.majorList || node.classList || []).map(childNode => formatNode(childNode, level + 1))
    }

    return formattedNode
  }

  return data.map(node => formatNode(node, 1))
}

//处理机构数据(到学生层级)
export function convertStudentArrayWithCustomNames(data, isAddStudentNum) {
  let treeData = []

  function getNameById(id, type) {
    switch (type) {
      case 'branch':
        return data.find(item => item.branchId === id).branchName
      case 'major':
        return data.find(item => item.majorId === id).majorName
      case 'class':
        return data.find(item => item.classId === id).className
      case 'student':
        return data.find(item => item.studentId === id).studentName
      default:
        return 'Unknown'
    }
  }

  data.forEach(item => {
    let branchIndex = treeData.findIndex(branch => branch.id === item.branchId)

    if (branchIndex === -1) {
      treeData.push({
        id: item.branchId,
        name: getNameById(item.branchId, 'branch'),

        children: []
      })
      branchIndex = treeData.length - 1
    }

    let majorIndex = treeData[branchIndex].children.findIndex(major => major.id === item.majorId)

    if (majorIndex === -1) {
      treeData[branchIndex].children.push({
        id: item.majorId,
        name: getNameById(item.majorId, 'major'),
        children: []
      })
      majorIndex = treeData[branchIndex].children.length - 1
    }

    let classIndex = treeData[branchIndex].children[majorIndex].children.findIndex(classItem => classItem.id === item.classId)

    if (classIndex === -1) {
      treeData[branchIndex].children[majorIndex].children.push({
        id: item.classId,
        name: getNameById(item.classId, 'class'),
        children: []
      })
      classIndex = treeData[branchIndex].children[majorIndex].children.length - 1
    }

    let studentName = getNameById(item.studentId, 'student')
    console.log(item, 'klklklklklklklk=-=-')
    treeData[branchIndex].children[majorIndex].children[classIndex].children.push({
      id: item.studentId,
      disabled: !item.enabled,
      name: studentName + `${isAddStudentNum ? `(${item.studentNum})` : ''}`
    })
  })

  return treeData
}

//处理机构数据(到班级层级)
export function formatArrayWithCustomNames(data) {
  let treeData = []

  function getNameById(id, type) {
    switch (type) {
      case 'branch':
        return data.find(item => item.branchId === id).branchName
      case 'major':
        return data.find(item => item.majorId === id).majorName
      case 'class':
        return data.find(item => item.classId === id).className
      default:
        return 'Unknown'
    }
  }

  data.forEach(item => {
    let branchIndex = treeData.findIndex(branch => branch.id === item.branchId)

    if (branchIndex === -1) {
      treeData.push({
        id: item.branchId,
        name: getNameById(item.branchId, 'branch'),
        children: []
      })
      branchIndex = treeData.length - 1
    }

    let majorIndex = treeData[branchIndex].children.findIndex(major => major.id === item.majorId)

    if (majorIndex === -1) {
      treeData[branchIndex].children.push({
        id: item.majorId,
        name: getNameById(item.majorId, 'major'),
        children: []
      })
      majorIndex = treeData[branchIndex].children.length - 1
    }

    let classIndex = treeData[branchIndex].children[majorIndex].children.findIndex(classItem => classItem.id === item.classId)

    if (classIndex === -1) {
      treeData[branchIndex].children[majorIndex].children.push({
        id: item.classId,
        disabled: !item.enabled,
        name: getNameById(item.classId, 'class')
      })
    }
  })

  return treeData
}



